package com.notetool.util.tokenparser.syntax;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.TreeSet;

import com.notetool.util.tokenparser.Token;
import com.notetool.util.tokenparser.semantic.ParserSemanticException;
import com.notetool.util.tokenparser.semantic.TokenRestriction;

public abstract class Model
{
	
    ////////////////////
    // Members
    private ArrayList<TokenRestriction> restrictions;
    private ArrayList<WordModel> preferedNext;
    private TreeSet<String> classes;
	
    ////////////////////
    // Constructors
	public Model ()
	{
		restrictions = new ArrayList<TokenRestriction> ();
		preferedNext = new ArrayList<WordModel> ();
		classes = new TreeSet<String> ();
	}
	
    ////////////////////
    // Accessors
	public ArrayList<WordModel> getPreferedNext ()
	{
	    return preferedNext;
	}
	
    ////////////////////
    // Abstract methods
    
    ////////////////////
    // Public methods
    public void validateSemantics (Token token) throws ParserSemanticException
    {
        for (TokenRestriction restriction : restrictions)
            restriction.checkRestriction (token);
    }
    
    public Model addRestriction (TokenRestriction restriction)
    {
        if (restriction == null)
            return this;
        
        if (!restrictions.contains (restriction))
            restrictions.add (restriction);
        
        return this;
    }
    
    public Model addClass (String... clsses)
    {
        for (String curClass : clsses)
            if (curClass != null)
                classes.add (curClass);
        
        return this;
    }
    
    public Model addPreferedNext (WordModel... mdls)
    {
        for (WordModel mdl : mdls)
            if (mdl != null)
                preferedNext.add (mdl);
        
        return this;
    }
    
    public boolean is (String cls)
    {
        return classes.contains (cls);
    }
    
    public Iterator<String> getClasses ()
    {
        return classes.iterator ();
    }
    
    public String[] classesArray ()
    {
        return classes.toArray (new String[0]);
    }
	
}
